(function () {
    'use strict';
	
	var redis  = require('redis'),
		client = redis.createClient();
	
	function mostrarErrorYSalir(err, codigoError) {
		console.log(err);
		terminarTodo(codigoError);
	}
	
	function terminarTodo(codigoError) {
		console.log('terminando cliente...');
		client.quit();
		console.log('terminando node...');
		console.log('exit code=' + codigoError);
		process.exit(codigoError);
	}
	
	client.on('error', function (err) {
		console.log('ev error');
		console.log(err);
		terminarTodo(1);
	});
	
	client.on('connect', function () {
		console.log('ev connect');
	});
	
	client.on('ready', function () {
		console.log('ev ready');
		var fchain = [
			function () {
				client.set('avarela.email', 'hav29a@gmail.com', fchain.shift());
			},
			function (err, reply) {
				if (err) {
					mostrarErrorYSalir(1);
				} else {
					console.log("reply: " + reply);
					client.get('avarela.email', fchain.shift());
				}
			},
			function (err, reply) {
				if (err) {
					mostrarErrorYSalir(1);
				} else {
					console.log("reply: " + reply);
					client.save(fchain.shift());
				}
			},
			function (err, reply) {
				if (err) {
					mostrarErrorYSalir(1);
				} else {
					console.log("reply: " + reply);
					terminarTodo(0);
				}
			}
		];
		fchain.shift()();
	});
}());
